/**
 * Copyright (c) 2007, Ashish Lijhara
 * All rights reserved.
 *
 * GlassPainter.java
 *
 */

package crazy.java.Aetas;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author  Ashish (Java Crazy)
 */
public class GlassPainter extends javax.swing.JComponent implements MouseListener{
    BufferedImage sun;
    BufferedImage moon;
    BufferedImage pict;
    BufferedImage imageA;
    Calendar a;
    Calendar c;
    BufferedImage imageB;
    int run=0;
    Point point;
    Point inPoint1;
    Point inPoint2;
    Point inPoint3;
    Point inPoint4;
    Point caPoint1;
    Point caPoint2;
    Point caPoint3;
    Point caPoint4;
    javax.swing.JFrame frame;
    
    /** Creates new form GlassPainter */
    
    public GlassPainter(javax.swing.JFrame f) {
        point=new Point();
        inPoint1=new Point( 75,135);
        inPoint2=new Point(306,145);
        inPoint3=new Point( 75,460);
        caPoint1=new Point(330,140);
        caPoint2=new Point(581,140);
        caPoint3=new Point(330,467);
        a=Calendar.getInstance();
        initComponents();
        setOpaque(false);
        addMouseListener(this);
        frame=f;
        timer.schedule(clockTask, 0, 10000);
    }
    Timer timer = new Timer();
    TimerTask clockTask=new TimerTask() {
        public void run() {
            updateTime();
            run=0;
            repaint();
        }
    };
    public void mouseExited(MouseEvent evt) { }
    public void mouseEntered(MouseEvent evt) { }
    public void mouseClicked(MouseEvent evt) {
        
    }
    public void mouseReleased(MouseEvent evt) {
        point.x=evt.getX();
        point.y=evt.getY();
        if(point.x >= inPoint1.x && point.x <= inPoint2.x){
            if(point.y >= inPoint1.y && point.y <= inPoint3.y){
                repaint();
            }
        }
        if(point.x >= caPoint1.x && point.x <= caPoint2.x){
            if(point.y >= caPoint1.y && point.y <= caPoint3.y){
                repaint();
            }
        }
        
        
    }
    public void mousePressed(MouseEvent evt) {}
    
    public void doMinimizeAnimation(){
        AnimationHandler ani=new AnimationHandler(frame);
        ani.minimize();
        
        
    }
    
    private void updateTime() {
        
        c=Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT-06:00"));
        a=Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT+05:30"));
    }
    
    /** This method is called from within the constructor to
     * initialize the GlassPane.
     */
    private void initComponents() {
        javax.swing.JButton jButton1;
        javax.swing.JButton jButton2;
        
        jButton1 = new ImageButton(new ImageIcon("res/min.png"),new ImageIcon("res/min_d.png"),new ImageIcon("res/min_h.png"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doMinimizeAnimation();
            }
        });
        jButton1.setToolTipText("Switch To MiniFrame Mode");
        jButton2 = new ImageButton(new ImageIcon("res/close.png"),new ImageIcon("res/close_d.png"),new ImageIcon("res/close_h.png"));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                System.exit(0);
            }
        });
        jButton2.setToolTipText("Exit");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap(340, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jButton2)
                .addComponent(jButton1))
                .addContainerGap(280, Short.MAX_VALUE))
                );
    }
    /**
     *
     * @param g
     */
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d=(Graphics2D)g;
        BufferedImage image= new BufferedImage(getWidth(), getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        
        Graphics2D g2=image.createGraphics();
        
        GradientPaint p=new GradientPaint(0,0,new Color(1.0f,1.0f,1.0f,1.0f),0,30,new Color(1.0f,1.0f,1.0f,0.1f));
        g2.setPaint(p);
        g2.fillRoundRect(573,13,60,25,4,4);
        
        AlphaComposite c=AlphaComposite.getInstance(AlphaComposite.SRC_IN,0.0f);
        g2.setComposite(c);
        p=new GradientPaint(0,0,new Color(1.0f,1.0f,1.0f,0.0f),0,30,new Color(1.0f,1.0f,1.0f,0.0f));
        g2.setPaint(p);
        Ellipse2D r= new Ellipse2D.Float(579,17,20,17);
        g2.fill(r);
        r= new Ellipse2D.Float(607,17,20,17);
        g2.fill(r);
        g2d.drawImage(image, null, 0, 0);
        
        
        try {
            File file = new File("res/gloss.png");
            imageA = ImageIO.read(new URL(file.toURI().toURL().toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        try {
            File file = new File("res/gloss2.png");
            imageB = ImageIO.read(new URL(file.toURI().toURL().toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        if(run==0){
            g2d.drawImage(imageA, null, 75, 135);
            g2d.drawImage(imageB, null, 330, 140);
            run++;
        } else {
            if(point.x >= inPoint1.x && point.x <= inPoint2.x){
                if(point.y >= inPoint1.y && point.y <= inPoint3.y){
                    
                    g2d.drawImage(imageA, null, 75, 135);
                    g2d.drawImage(imageB, null, 330, 140);
                    //run=3;
                }
            }
            if(point.x >= caPoint1.x && point.x <= caPoint2.x){
                if(point.y >= caPoint1.y && point.y <= caPoint3.y){
                    g2d.drawImage(imageB, null, 75, 135);
                    g2d.drawImage(imageA, null, 330, 140);
                    //run=4;
                }
            }
        }
        
        g2d.setFont(FontUtil.getCompatiableFont("res/TRASHED.ttf",15f));
        g2d.setColor(new Color(42,50,68));
        g2d.drawString("INDIA",85,160);
        new GradientUtils(g2d,82,143,60,18,0,0,GradientUtils.RECT_GRADIENT);
        g2d.setColor(new Color(42,50,68));
        
        g2d.drawString("Central America",340,160);
        new GradientUtils(g2d,335,143,140,18,0,0,GradientUtils.RECT_GRADIENT);
        
        drawSunMoon(g2d);
        try {
            File file = new File("res/pict.png");
            pict = ImageIO.read(new URL(file.toURI().toURL().toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        g2d.drawImage(pict, null,320,0);
    }
    /**
     *
     * @param g2d
     */
    public void drawSunMoon(Graphics2D g2d){
        
        try {
            File file = new File("res/Sun.png");
            sun = ImageIO.read(new URL(file.toURI().toURL().toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        
        try {
            File file = new File("res/Moon.png");
            moon = ImageIO.read(new URL(file.toURI().toURL().toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        String zone = java.util.TimeZone.getDefault().getDisplayName();
        int hr1;
        int time1;
        int hr2;
        int time2;
        c =Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT-06:00"));
        if(zone.equals("GMT+05:30")){
            
            hr1=a.get(Calendar.HOUR_OF_DAY);
            
            hr2=c.get(Calendar.HOUR_OF_DAY);
            
            time1=hr1*100;
            time2=hr2*100;
            if(time1>=1900 || time1 < 600){g2d.drawImage(moon, null,205, 80);} else{g2d.drawImage(sun, null,205, 80);}
            if(time2>=1900 || time2 < 600){g2d.drawImage(moon, null,475, 90);} else{g2d.drawImage(sun, null,475, 90);}
            g2d.setFont(FontUtil.getCompatiableFont("res/TRASHED.ttf",15f));
            g2d.setColor(new Color(42,50,68));
            if(time1 >= 0000 && time1 < 1200){
                
                g2d .drawString("AM",260,240);
                new GradientUtils(g2d,258,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            } else{
                g2d .drawString("PM",260,240);
                new GradientUtils(g2d,258,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            }
            g2d.setColor(new Color(42,50,68));
            if(time2 >= 0000 && time2 < 1200){
                
                g2d .drawString("AM",510,240);
                new GradientUtils(g2d,509,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            } else{
                g2d .drawString("PM",510,240);
                new GradientUtils(g2d,509,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            }
            
        }else{
            hr1=a.get(Calendar.HOUR_OF_DAY);
            
            hr2=c.get(Calendar.HOUR_OF_DAY);
            
            time1=hr1*100;
            time2=hr2*100;
            if(time2>=1900 || time2 < 600){g2d.drawImage(moon, null,205, 80);} else{g2d.drawImage(sun, null,205, 80);}
            if(time1>=1900 || time1 < 600){g2d.drawImage(moon, null,475, 90);} else{g2d.drawImage(sun, null,475, 90);}
            g2d.setFont(FontUtil.getCompatiableFont("res/TRASHED.ttf",15f));
            g2d.setColor(new Color(42,50,68));
            if(time2 >= 0000 && time2 < 1200){
                
                g2d .drawString("AM",260,240);
                new GradientUtils(g2d,258,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            } else{
                g2d .drawString("PM",260,240);
                new GradientUtils(g2d,258,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            }
            g2d.setColor(new Color(42,50,68));
            if(time1 >= 0000 && time1 < 1200){
                
                g2d .drawString("AM",510,240);
                new GradientUtils(g2d,509,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            } else{
                g2d .drawString("PM",50,240);
                new GradientUtils(g2d,509,222,32,18,0,0,GradientUtils.RECT_GRADIENT);
            }
        }
    }
}

